﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;

using System.Text;
using System.Text.RegularExpressions;
using Hangjing.SQLServerDAL.serverinterface;
using Hangjing.Weixin;
using Hangjing.Common;

/// <summary>
/// 提现
/// </summary>
public partial class AndroidAPI_user_widthdraw : APIPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        string openid = WebUtility.InputText(Request["openid"]);
        string userid = WebUtility.InputText(Request["userid"]);
        decimal price = WebUtility.InputText(Request["price"], 'c');

        ECustomerInfo user = new ECustomer().GetModel(Convert.ToInt32(userid));
        if (user == null)
        {
            rs.msg = "用户不存在";
            sendToClient();
            Response.End();
            return;
        }

        if (user.PayPWDQuestion != openid)
        {
            rs.msg = "openid异常";
            sendToClient();
            Response.End();
            return;
        }

        if (price > 200 || price < 1)
        {
            rs.msg = "提现金额只能是1-200之间";
            sendToClient();
            Response.End();
            return;
        }

        UserShareIncomeDal userShareIncomeDal = new UserShareIncomeDal();

        user_share_income user_Share_Income = userShareIncomeDal.getUserLimit(user.DataID);
        if (user_Share_Income.user_limit < 100)
        {
            rs.msg = "超过100可提现";
            sendToClient();
            Response.End();
            return;
        }
        if (user_Share_Income.user_limit < price)
        {
            rs.msg = "余额不足";
            sendToClient();
            Response.End();
            return;
        }


        user_share_income_log log = new user_share_income_log();
        log.c_t = DateTime.Now;
        log.u_t = DateTime.Now;
        log.source = 10;
        log.msg = "发起提现";
        log.user_limit = 0;
        log.user_id = user.DataID;
        log.from_user_id = 0;
        log.change_limit = price;

        apiResultInfo apiResult = userShareIncomeDal.changeLimit(log, LimitOperation.sub);
        if (apiResult.state == 0)
        {
            rs.msg = apiResult.msg;
            sendToClient();
            Response.End();
            return;
        }
        string partner_trade_no = Utils.getTimestamp() + "a" + user.DataID;

        apiResultInfo transferResult = new WechatPay2User(Context).Pay(openid, price, partner_trade_no);
        if (transferResult.state == 0)
        {
            log.c_t = DateTime.Now;
            log.u_t = DateTime.Now;
            log.source = 11;
            log.msg = "提现失败，退款";
            log.user_limit = 0;
            log.user_id = user.DataID;
            log.from_user_id = 0;
            log.change_limit = price;

            apiResult = userShareIncomeDal.changeLimit(log, LimitOperation.add);

            rs.state = 0;
            rs.msg = "服务器繁忙，请稍候再试";
        }
        else
        {
            rs.state = 1;
            rs.msg = "提现成功，请注意查收微信零钱";
        }

        Response.Write(sendToClient());
        Response.End();



    }
}
